﻿using PEPe.DataAccessModel;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PEPe.ADO
{
    public class ProfissionalADO
    {
        
        public Profissional Consulta(int profissionalId)
        {
            using (var db = new PEPeEntities())
            {
                return db.Profissional.AsQueryable().Where(x => x.Id == profissionalId).FirstOrDefault();
            }

        }



        public IEnumerable<Profissional> ListarProfissionalPaginado(string searsh, int skip, int take)
        {
            IEnumerable<Profissional> lista = null;
            using (var db = new PEPeEntities())
            {

                if (string.IsNullOrEmpty(searsh))
                    lista = db.Profissional.OrderBy(x => x.Nome);
                else
                    lista = (from item in db.Profissional
                             where item.Nome.ToUpper().Contains(searsh.ToUpper())
                             orderby item.Nome
                             select item);

                return lista.Skip(skip).Take(take).ToList();
            }

        }


        public int TotalRegistros(string searsh)
        {
            using (var db = new PEPeEntities())
            {
                int total = 0;

                if (string.IsNullOrEmpty(searsh))
                    total = db.Profissional.Count();
                else
                    db.Profissional.Where(x => x.Nome.ToUpper().Contains(searsh.ToUpper())).Count();


                return total;
            }

        }

    
        public void Salvar(Profissional profissional)
        {
            using (var db = new PEPeEntities())
            {
                if (profissional.Id == 0)
                {
                    db.Profissional.Add(profissional);
                }
                else
                {
                    Profissional upd = db.Profissional.Find(profissional.Id);
                    db.Entry(upd).CurrentValues.SetValues(profissional);
                }
                db.SaveChanges();
            }
        }


        public void Excluir(int Id)
        {
            using (var db = new PEPeEntities())
            {
                Profissional profissional = db.Profissional.Find(Id);

                db.Profissional.Remove(profissional);
                db.SaveChanges();
            }
        }
    }
}
